<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <script src="./lib/vue.js"></script>
        <script src="./lib/swiper/js/swiper-bundle.min.js"></script>
        <link rel="stylesheet" href="./lib/swiper/css/swiper-bundle.min.css" />
        <title>19 vue-swiper轮播组件</title>
        <style>
            .swiper-container {
                width: 600px;
                height: 300px;
            }
        </style>
    </head>
    <body>
        <div id="box">
            <swiper :key="dataList.length">
                <div
                    v-for="(data, index) in dataList"
                    class="swiper-slide"
                    v-swiper="{
                    index: index,
                    length: dataList.length - 1
                }"
                >
                    {{ data }}
                </div>
            </swiper>
        </div>

        <script>
            Vue.directive('swiper', {
                inserted(el, bind) {
                    if (bind.value.index === bind.value.length) {
                        const mySwiper = new Swiper('.swiper-container', {
                            // direction: 'vertical', // 垂直切换选项
                            autoplay: true, // 设置自动循环播放
                            loop: true, // 循环模式选项
                            // 如果需要分页器
                            pagination: {
                                el: '.swiper-pagination'
                            },
                            // 如果需要前进后退按钮
                            navigation: {
                                nextEl: '.swiper-button-next',
                                prevEl: '.swiper-button-prev'
                            },
                            // 如果需要滚动条
                            scrollbar: {
                                el: '.swiper-scrollbar'
                            }
                        });
                    }
                }
            });

            Vue.component('swiper', {
                template: `<div class="swiper-container">
                    <div class="swiper-wrapper">
                        <slot></slot>
                    </div>
                    <!-- 如果需要分页器 -->
                    <div class="swiper-pagination"></div>
                    <!-- 如果需要导航按钮 -->
                    <div class="swiper-button-prev"></div>
                    <div class="swiper-button-next"></div>
                    <!-- 如果需要滚动条 -->
                    <div class="swiper-scrollbar"></div>
                </div>`,
                mounted() {
                    // const mySwiper = new Swiper('.swiper-container', {
                    //     // direction: 'vertical', // 垂直切换选项
                    //     autoplay: true, // 设置自动循环播放
                    //     loop: true, // 循环模式选项
                    //     // 如果需要分页器
                    //     pagination: {
                    //         el: '.swiper-pagination'
                    //     },
                    //     // 如果需要前进后退按钮
                    //     navigation: {
                    //         nextEl: '.swiper-button-next',
                    //         prevEl: '.swiper-button-prev'
                    //     },
                    //     // 如果需要滚动条
                    //     scrollbar: {
                    //         el: '.swiper-scrollbar'
                    //     }
                    // });
                }
            });

            const vm = new Vue({
                el: '#box',
                data() {
                    return {
                        dataList: []
                    };
                },
                mounted() {
                    setTimeout(() => {
                        this.dataList = ['side 01', 'side 02', 'side 03'];
                    }, 2000);
                }
            });
        </script>
    </body>
</html>
